Skip to content

fix: allow transport restart after close()#1647

Closed
matantsach wants to merge 3 commits intomodelcontextprotocol:mainfrom
matantsach:fix/1641-transport-restart-after-close
Closed

fix: allow transport restart after close()#1647
matantsach wants to merge 3 commits intomodelcontextprotocol:mainfrom
matantsach:fix/1641-transport-restart-after-close

Conversation

@matantsach
Copy link
Copy Markdown
Contributor

Summary

Fixes #1641. StreamableHTTPClientTransport and SSEClientTransport cannot be restarted after close() because internal guard variables are not reset to undefined.

Changes

  • StreamableHTTPClientTransport.close(): Reset _abortController to undefined after aborting, so start() can create a fresh controller
  • SSEClientTransport.close(): Reset _abortController, _eventSource, and _endpoint to undefined — clears both start guards and avoids stale endpoint URLs on restart

Testing

  • Added lifecycle test for StreamableHTTPClientTransport: start → close → start → send → verify functional
  • Added lifecycle test for SSEClientTransport: start → close → start → send → verify functional
  • All existing tests pass (pnpm test:all, pnpm check:all)

close() aborts _abortController but never resets it to undefined, so
start() sees a truthy guard and throws "already started." Same issue in
SSEClientTransport with _eventSource. Also reset _endpoint in SSE to
avoid stale endpoint URLs after restart.

Fixes modelcontextprotocol#1641

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@matantsach matantsach requested a review from a team as a code owner March 9, 2026 10:25
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 9, 2026

🦋 Changeset detected

Latest commit: d4a7f1f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 9, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1647

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1647

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1647

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1647

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1647

commit: d4a7f1f

@matantsach
Copy link
Copy Markdown
Contributor Author

Hi — just checking in. All CI is green (build, tests across Node 20/22/24, Bun, Deno, conformance) and the changeset is included. Let me know if there's anything to adjust or if there are design concerns with the approach.

@felixweinberger
Copy link
Copy Markdown
Contributor

Thanks for this. The fix is right but the PR is conflicting with ~400 lines of test additions on main. Opening a fresh PR with the same approach plus clearing _sessionId in close() as well (stale session ID would 404 on the first post-restart send). Will credit you there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StreamableHTTPClientTransport cannot be restarted after close() — breaks OAuth re-authentication

2 participants